<template>
  <div id="container">
    <div class="header">帅哥</div>
    <ul class="content clear-fix">
      <li
        v-for="(item, index) in dataInfo"
        :key="'info' + index"
        class="eachInfo"
      >
        <!-- 使用template标签，当item.isMe为true则表示当前消息是我说的，配合v-if标签使用 -->
        <template v-if="item.isMe">
          <img src="../assets/right.png" class="fr" /><span class="fr f-info">{{
            item.info
          }}</span>
        </template>

        <template v-else>
          <img src="../assets/left.png" class="fl" /><span class="fl f-info">{{
            item.info
          }}</span>
        </template>
      </li>
    </ul>
    <div class="footer">
      <div id="user_face_icon">
        <img src="../assets/right.png" alt="" />
      </div>
      <input
        type="text"
        @keyup.enter="sendMsgToB"
        v-model.trim.lazy="msgA"
        placeholder="说点什么吧..."
      />
      <span class="send" @click="sendMsgToB">发送</span>
    </div>
  </div>
</template>
import BScroll from "better-scroll"
<script>
export default {
  data() {
    return {
      // msgA是与输入框使用v-model进行双向绑定
      // dataInfo 是聊天记录
      msgA: "",
      dataInfo: [],
    };
  },
  created() {
    // 在js数据加载之后监听事件，并接收参数
    this.$eventBus.$on("AgetInfoFromMain", (val) => {
      this.dataInfo = val;
    });
  },

  methods: {
    // 当用户触发事件后就触发兄弟组件的getInfoFromA事件
    sendMsgToB() {
      if (!this.msgA) {
        alert("请输入非空内容！");
        return;
      }
      this.$eventBus.$emit("getInfoFromA", this.msgA);
      this.msgA = "";
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#container {
  width: 23%;
  height: 100%;
  background: #eee;
  box-shadow: 20px 20px 55px #777;
  border-radius: 0 0 5px 5px;
}

.header {
  background: #000;
  height: 10%;
  color: #fff;
  line-height: 40px;
  font-size: 16px;
  padding: 0 10px;
  border-radius: 5px 5px 0 0;
}
#user_face_icon {
  display: inline-block;
  width: 30px;
  height: 30px;
  border-radius: 30px;
  cursor: pointer;
}
.eachInfo {
  height: 38px;
  margin-bottom: 20px;
  line-height: 38px;
}
img {
  width: 30px;
  height: 30px;
  display: block;
  border-radius: 10px;
}
.content {
  font-size: 14px;
  height: 80%;
  overflow: auto;
  padding: 5px;
  word-wrap: break-word;
}
.fl {
  float: left;
  max-width: 55%;
  background-color: #7cfc00;
}
.f-info {
  margin: 0 8px;
  border: 1px solid #ccc;
  border-radius: 8px;
  box-shadow: 0 0 3px #ccc;
  padding: 0 4px;
}

.fr {
  float: right;
  max-width: 55%;
  background-color: #7cfc00;
}
.clear-fix::after {
  content: "";
  display: block;
  clear: both;
}
.footer {
  height: 10%;
  background-color: #666;
  display: flex;
  padding: 0 10px;
  justify-content: space-between;
  align-items: center;
}
.user_face_icon img {
  display: block;
  margin-left: 15px;
}
.footer input {
  height: 25px;
  width: 70%;
  outline: none;
  font-size: 16px;

  text-indent: 10px;
  border-radius: 6px;
  right: 80px;
}
.footer .send {
  width: 40px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border-radius: 10px;
  background-color: #00ff00;
}
</style>
